/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Entities;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author JackOddie
 */
public class DBConnection {

    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:XE";
    String userid = "rpltr";
    String password = "rpltr";
    Connection conn;
    Statement stmt;
    ResultSet rset;
    String query;

    public DBConnection() {
    }

    public DBConnection(String jdbcUrl, String userid, String password) {
        this.jdbcUrl = jdbcUrl;
        this.userid = userid;
        this.password = password;
    }

    public void getDBConnection() throws SQLException {
        OracleDataSource ds;
        ds = new OracleDataSource();
        ds.setURL(jdbcUrl);
        conn = ds.getConnection(userid, password);
    }

    public ResultSet select(String query) throws SQLException {
        this.getDBConnection();
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rset = stmt.executeQuery(query);
        return rset;
    }

//    public User getAllDataById(int id) throws SQLException {
//        User user = new User();
//        String query = "select * from FB_USER where user_id='" + id + "'";
//        ResultSet rset = select(query);
//        user.id = id;
//        while (rset.next()) {
//            user.first_name = rset.getString("FIRST_NAME");
//            user.last_name = rset.getString("LAST_NAME");
//            user.birth_date = rset.getString("BIRTH_DATE");
//            user.email = rset.getString("EMAIL");
//            user.sex = rset.getString("SEX");
//            user.imageUrl = rset.getString("IMAGEURL");
//        }
//        return user;
//    }

    public void updateData(String query) throws SQLException {
        this.getDBConnection();
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        stmt.executeUpdate(query);
    }

    public void execute(String query) throws SQLException {
        this.getDBConnection();
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
        stmt.execute(query);
    }
//
//    public ResultSet getDataOf(String firstName, String lastName, String email) throws SQLException {
//        String query = "select * from fb_user where first_name='" + firstName + "' OR last_name = '" + email + "' OR email='" + lastName + "'";
//        return select(query);
//    }
}
